using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using KingSite.Shop.Service;
using KingSite.Shop.Core.Service;
using KingSite.Shop.Core.Mvc;
using KingSite.Shop.Domain;
using KingSite.Shop.Core.Service.Model;


namespace KingSite.Shop.Web.Controllers
{
    public class GoodsController : BaseController
    {
        IGoodsService _service;
        IGoodsSingleAttributeService _singleAttributeService;
        IGoodsManyAttributeService _manyAttributeService;
        ICategoryService _categoryService;

        public GoodsController(IGoodsService service, IGoodsSingleAttributeService singleAttributeService, IGoodsManyAttributeService manyAttributeService, ICategoryService categoryService) {
            _service = service;
            _singleAttributeService = singleAttributeService;
            _manyAttributeService = manyAttributeService;
            _categoryService = categoryService;             
        }
        //
        // GET: /Goods/
        
        public ActionResult Index()
        {
            return RedirectToAction("List");                
        }

        public ActionResult List(string id) {
            //int cid = 0;
            //int pid = cid;
            //if (!string.IsNullOrEmpty(id)) {
            //    string[] all = id.Split('-');
            //    string[] ids =new string[3];
            //    for (int i = 0; i <= 2; i++) {
            //        ids[i] = all[i];
            //    }
            //    int ShowType = Convert.ToInt32(all[3]);
            //    ViewData["ShowType"] = ShowType;
            //    foreach (string s in ids) {
            //        if (Convert.ToInt32(s) > 0) {
            //            cid = Convert.ToInt32(s);
            //        }
            //    }

            //    foreach (string s in ids) {
            //        if (Convert.ToInt32(s) > 0) {
            //            pid = Convert.ToInt32(s);
            //            break;
            //        }
            //    }
            //}
            int pid;
            GoodsListShowType ShowType ;
            IList<Goods> list = _service.GetGoodsListByUrlParamater(id, out pid,out ShowType);
            ViewData.Model = list;
            ViewData["CateogryId"] = pid;
            ViewData["ShowType"] = ShowType;
            return View();
        }
        
        public ActionResult Detail(int id) {
            Goods goods = _service.Find(id);
            ViewData.Model = goods;
            int CategoryId = _service.GetParentCategoryIdByGoodsId(id);
            
            ViewData["CateogryId"] = CategoryId;

            IList<GoodsSingleAttribute> SingleAttributeList = _singleAttributeService.FindAll(id);
            ViewData["SingleAttributeList"] = SingleAttributeList;

            IList<GoodsManyAttribute> ManyAttributeNameList = _manyAttributeService.FindManyAttributeName(id);
            ViewData["ManyAttributeNameList"] = ManyAttributeNameList;

            IList<GoodsManyAttribute> ManyAttributeList = _manyAttributeService.FindAll(id);
            ViewData["ManyAttributeList"] = ManyAttributeList;

            return View();
        }



    }
}
